<template>
    <section>
        <!-- 搜索 -->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getUsers">查询</el-button>
				</el-form-item>
				<!-- <el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item> -->
			</el-form>
    </el-col>
    <el-table :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;" border>
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
            <!-- <el-table-column prop="u_img" label="头像" width="120" sortable>
			</el-table-column> -->
			<el-table-column prop="u_name" label="姓名" width="120" sortable>
			</el-table-column>
			<el-table-column prop="u_sex" label="性别" width="100" :formatter="formatSex" sortable>
			</el-table-column>
			<el-table-column prop="u_age" label="年龄" width="100" sortable>
			</el-table-column>
            <el-table-column prop="u_status" label="状态" width="100" :formatter="formatStatus" sortable>
			</el-table-column>
            <el-table-column prop="u_email" label="邮箱" width="160" sortable>
			</el-table-column>
			<el-table-column prop="u_number" label="电话" width="160" sortable>
			</el-table-column>
			<el-table-column prop="u_address" label="地址" min-width="160" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template  scope="scope">
                    
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">
                        编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">
                        删除</el-button>
				</template>
			</el-table-column>
		</el-table>
        <el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination 
            layout="total, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange" 
            :page-size="pagesize"
            :current-page=1
            :total="total" 
            style="float:right;">
			</el-pagination>
		</el-col>
    </section>
</template>

<script>
export default {
data () {
    return {
        filters:{
            name:""
        },
        users:[],
        page:1,
        listLoading:false,
        sels:[],
        pagesize:10,
        total:0,
        size:6
    }
},
methods: {
    // 获取用户列表
    getUsers()
    {
        console.log(this.filters.name)
        
        let para = {
					page: this.page,
                    limit:this.pagesize,
                    name:this.filters.name
                };
                this.listLoading = true;
        this.$axios.post("http://127.0.0.1:8081/admin/usermsg",para)
        .then(res=>
        {
            this.users=res.data.data;
            this.total=res.data.count
            // console.log(this.total)
            this.listLoading = false;
            // console.log(res.data.data);
             console.log("user",this.users);
        })
       
        
    },
    handleAdd()
    {
        console.log("添加");
    },
    selsChange(sels)
    {
        console.log(sels)
        this.sels=sels;
    },
    formatSex(row,column)
    {
        return row.u_sex == 2 ? '男' : row.u_sex == 1 ? '女' : '未知';
    },
    formatStatus(row,column)
    {
        return row.u_status == 1 ? '1' : row.u_status == 0 ? '0' : '未知';
    },
    handleEdit(index,row)
    {
        console.log("编辑");
    },
    handleDel(index,row)
    {
        let names=row.u_name;
        // console.log("删除");
        this.$confirm("确认删除选中记录吗？","提示",{
            type: 'warning'
        }).then(()=>
        {
            
            this.listLoading=true;
            let para={names:names};
            // para=names;
            this.$axios.post("http://127.0.0.1:8081/admin/deluser",para).then(res=>
            {
                this.listLoading=false;
                let ress=res.msg
                this.$message({
                    message:res.data.msg,
                    type:"success"
                })
                this.getUsers();
            })
           
        }).catch(()=>
        {

        })
    },
    batchRemove()
    {
        let names=this.sels.map(item => item.u_name).toString();
        this.$confirm("确认删除选中记录吗？","提示",{
            type: 'warning'
        }).then(()=>
        {
            
            this.listLoading=true;
            let para={names:names};
            // para=names;
            this.$axios.post("http://127.0.0.1:8081/admin/deluser",para).then(res=>
            {
                this.listLoading=false;
                let ress=res.msg
                this.$message({
                    message:res.data.msg,
                    type:"success"
                })
                this.getUsers();
            })
           
        }).catch(()=>
        {

        })
    },
    handleCurrentChange(val)
    { 
        // console.log("val",val);
        this.page = val;
		this.getUsers();
    },
    handleSizeChange(val)
    {
        this.page = val;
		this.getUsers();
    }
    
},
mounted () {
    this.getUsers();
}
}
</script>

<style>

</style>
